package osirix.ce;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

public class Geolocalizacion extends Activity implements Runnable {

	LocationManager mLocationManager;
	Location mLocation;
	MyLocationListener mLocationListener;
	String outlat;
	String outlong;
    

	private ProgressDialog pd;
	Location currentLocation = null;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        writeSignalGPS();
	}

	private void writeSignalGPS() {

//		DialogInterface.OnCancelListener dialogCancel = new DialogInterface.OnCancelListener() {
//
//			public void onCancel(DialogInterface dialog) {
//				Toast.makeText(
//						getBaseContext(),
//						getResources().getString(R.string.gps_signal_not_found),
//						Toast.LENGTH_LONG).show();
//				handler.sendEmptyMessage(0);
//			}
//
//		};
//
//		pd = ProgressDialog.show(this,
//				this.getResources().getString(R.string.search), this
//						.getResources().getString(R.string.search_signal_gps),
//				true, true, dialogCancel);

		Thread thread = new Thread(this);
		thread.start();

	}

	public void run() {

		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

			Looper.prepare();

			mLocationListener = new MyLocationListener();

			mLocationManager.requestLocationUpdates(
					LocationManager.GPS_PROVIDER, 900000, 500, mLocationListener);
			Looper.loop();
			Looper.myLooper().quit();

		} else {

			Toast.makeText(getBaseContext(),
					getResources().getString(R.string.gps_signal_not_found),
					Toast.LENGTH_LONG).show();

		}
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			pd.dismiss();
			mLocationManager.removeUpdates(mLocationListener);
	    	if (currentLocation!=null) {
	    		outlat =("Latitude: " + currentLocation.getLatitude());
	    		outlong  = ("Longitude: " + currentLocation.getLongitude());
	    		ComparendoActivity comparendo = new ComparendoActivity();
	    		comparendo.getDatosComparendo().setLatitud(Double.toHexString(currentLocation.getLatitude()));
	    		comparendo.getDatosComparendo().setLongitud(Double.toString(currentLocation.getLongitude()));
	    	}
		}
	};
	
	private class MyLocationListener implements LocationListener 
    {
        public void onLocationChanged(Location loc) {
            if (loc != null) {
                Toast.makeText(getBaseContext(), 
                    getResources().getString(R.string.gps_signal_found), 
                    Toast.LENGTH_LONG).show();
                setCurrentLocation(loc);
                handler.sendEmptyMessage(0);
            }
        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }

        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
            // TODO Auto-generated method stub
        }
    }
	
	private void setCurrentLocation(Location loc) {
    	currentLocation = loc;
    }
	
	public Location getCurrentLocation(){
		return currentLocation;
	}
	
	public String getOutlat() {
		return outlat;
	}

	public void setOutlat(String outlat) {
		this.outlat = outlat;
	}
	
	public String getOutlong() {
		return outlong;
	}

	public void setOutlong(String outlong) {
		this.outlong = outlong;
	}

}
